#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
int judge(ull div) {
  int cnt=0;
  while(div!=0) {
    unsigned a = div%10U;
    cnt+=a;
    div /= 10;
  }
  return cnt;
}
int main() {
  int tc; cin>>tc;
  while(tc--) {
    ull n; cin>>n;
    if(n%2050==0)
      cout<<judge(n/2050)<<'\n';
    else
      cout<<"-1\n";
  }
}
